/**
 * 
 */
package org.vsg.stock.basic.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.vsg.common.biz.exception.ServiceException;
import org.vsg.stock.basic.domain.VolBalanceLine;
import org.vsg.stock.basic.domain.VolCharacterKlineDaily;
import org.vsg.stock.basic.persistence.VolCharacterKlineDailyDao;
import org.vsg.stock.basic.service.VolCharacterBusinussService;
import org.vsg.stock.basic.service.handler.VolCharacterKlineDailyHandlerManager;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.persistence.StockDailyKLineDao;

/**
 * @author Bill Vison
 *
 */
public class VolCharacterBusinussServiceImpl implements
		VolCharacterBusinussService {

	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");	
	
	@Resource
	private StockDailyKLineDao stockDailyKLineDao;
	
	@Autowired	
	private VolCharacterKlineDailyHandlerManager volCharacterKlineDailyHandlerManager;
	
	@Resource
	private VolCharacterKlineDailyDao volCharacterKlineDailyDao;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.VolCharacterBusinussService#updateOneStockCharacterMapToKlineDaily(java.lang.String, java.util.Date, java.util.Date)
	 */
	@Override
	public void updateOneStockCharacterMapToKlineDaily(String stockCode,
			Date startDate, Date endDate) throws ServiceException {
		
		List<StockDailyKLine> stkDailyKLineList = stockDailyKLineDao.findStockLineByCycle(stockCode, startDate, endDate);
		
		volCharacterKlineDailyHandlerManager.process( stkDailyKLineList , startDate ,  endDate);

	}

	@Override
	public List<VolBalanceLine> findVolGoldenLines(String stockCode,
			Date startDate, Date endDate) throws ServiceException {
		
		List<StockDailyKLine> stkDailyKLineList = stockDailyKLineDao.findStockLineByCycle(stockCode, startDate, endDate);		
		
		List<VolBalanceLine> volBalanceLineList = new Vector<VolBalanceLine>();
		
		// --- get the date index ---
		List<Date> dateIndexList = new Vector<Date>();
		for (StockDailyKLine daily : stkDailyKLineList) {
			dateIndexList.add( daily.getDate() );
		}
		
		
		List<VolCharacterKlineDaily> volCharacterKlineList = volCharacterKlineDailyDao.findVolCharacKlineDailyInCycleByStck(stockCode, startDate, endDate);
		
		VolCharacterKlineDaily currentKLineDaily = null;
		for (int i = 0 ; i < volCharacterKlineList.size() ; i++) {
			currentKLineDaily = volCharacterKlineList.get(i);
			int index = dateIndexList.indexOf( currentKLineDaily.getGoldenDate() );
			
			//System.out.println( "golden date : " + df.format( currentKLineDaily.getGoldenDate() ) );
			
			// --- 升级格柱 ---
			if ( currentKLineDaily.getLevelGolden() != VolCharacterKlineDaily.UPGRADE_TO_GOLDEN 
				&& currentKLineDaily.getLevelGolden() != VolCharacterKlineDaily.UPGRADE_CLOSE_GOLDEN) {
				
				int lastIndex = index - 1;
				int lastMark = i -1;
				if (lastMark > 0) {
					VolCharacterKlineDaily lastKLineDaily = volCharacterKlineList.get(lastMark);
					// --- 如果是 前一项目 ---
					if ( lastKLineDaily.getGoldenDate().equals( dateIndexList.get(lastIndex)) ) {
						//System.out.println("--------------------------");
						continue;
					}
				}
			}
			VolBalanceLine basicLine = locateBasicLineState(stkDailyKLineList ,currentKLineDaily, index , 0);
			
			volBalanceLineList.add( basicLine );

			//System.out.println( "**** golden line : " + df.format( basicLine.getAssoDate())  + " , price : " + basicLine.getPriceLine());		
		}
		
		return null;
	}
	
	private VolBalanceLine locateBasicLineState(List<StockDailyKLine> reps, VolCharacterKlineDaily charaKlineDaily, int index, int offset) {

		// --- case 1 , 三天在将军柱的实体上方 ---
		StockDailyKLine basicLine = reps.get( index );
		
		double compHighPrice = basicLine.getHigh();
		double compLowPrice = basicLine.getLow();
		double compTopPrice = basicLine.getOpen() > basicLine.getClose() ? basicLine.getOpen() : basicLine.getClose();
		double compButtonPrice = basicLine.getClose() < basicLine.getOpen() ? basicLine.getClose() : basicLine.getOpen();

		double tmpAtLeastPrice = 999999999;
		double tmpAtLeastClosePrice = 999999999;
		StockDailyKLine tmpKline = null;
		for (int i = 1; i < 4 ; i++) {
			tmpKline = reps.get( index + i);
			tmpAtLeastPrice = tmpKline.getLow() < tmpAtLeastPrice ? tmpKline.getLow() : tmpAtLeastPrice;
			tmpAtLeastClosePrice = tmpKline.getLow() < tmpAtLeastClosePrice ? tmpKline.getLow() : tmpAtLeastClosePrice;

		}
		
		VolBalanceLine line = null;
		// --- 黄金柱 , 次黄金柱 升级柱的处理方式 ----
		
		line = new VolBalanceLine();
		line.setStockCode( charaKlineDaily.getId().getStockCode() );
		line.setType( VolBalanceLine.GOLDEN_SUPPORT_LINE );
		line.setAssoDate( charaKlineDaily.getGoldenDate() );
		
		// --- 非升格升级柱 ---
		if ( charaKlineDaily.getLevelGolden() != VolCharacterKlineDaily.UPGRADE_CLOSE_GOLDEN 
			&& charaKlineDaily.getLevelGolden() !=VolCharacterKlineDaily.UPGRADE_TO_GOLDEN
		) {
			//  --- 取最高点 ---
			if ( tmpAtLeastPrice >= compHighPrice) {
				line.setPriceLine( compHighPrice );
			}
			//  --- 取实体上端 ----
			else if (tmpAtLeastPrice >= compTopPrice && tmpAtLeastPrice < compHighPrice) {
				line.setPriceLine( compTopPrice );
			}
			// --- 取实体中间 , 指到哪里最低 取哪里 ----
			else if (tmpAtLeastPrice >= compButtonPrice && tmpAtLeastPrice < compTopPrice) {
				line.setPriceLine( tmpAtLeastPrice );
			}
			// --- 取实体下端 ----
			else if (tmpAtLeastPrice >= compLowPrice && tmpAtLeastPrice < compButtonPrice ) {
				line.setPriceLine( compButtonPrice );
			}		
			// --- 取下引线 ----
			else if (tmpAtLeastPrice < compLowPrice) {
				line.setPriceLine( compLowPrice );					
			}				
		}
		// --- 升级的柱,取上有收盘 ---
		else {
			
			// --- 检查后面的最低价 ---
			double impValue = tmpAtLeastClosePrice > basicLine.getClose() ? basicLine.getClose() : tmpAtLeastClosePrice;
			line.setPriceLine( impValue );
		}		
	
		
		
		return line;
	}

	
	
}
